package com.bc.web.controller.org;


import com.bc.web.interceptors.AuthInterceptor;
import com.bc.web.model.MerchRole;
import com.bc.web.model.OrgUser;
import com.fs.common.extend.FSController;
import com.fs.common.util.FSDate;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 * 商户角色
 * @author zhongzhanfeng
 *
 */
public class MerchRoleController extends FSController{

	public void search(){
		Page<MerchRole> result = MerchRole.dao.search(super.getSearchCondition());
		setPageResultToRequest("items", result);
		render("merch/merchRoleList.html");
	}
	
	public void addPage(){
		render("merch/merchRoleAdd.html");
	}
	
	public void updatePage(){
		MerchRole role = MerchRole.dao.findById(getParaToInt("id"));
		setAttr("role", role);
		render("merch/merchRoleUpdate.html");
	}
	
	public void alloctSourcePage(){
		MerchRole role = MerchRole.dao.findById(getPara("id"));
		setAttr("item", role);
		setAttr("sourceList", AuthInterceptor.getSidebar_merch_zh_CN());
		render("merch/merchRoleAlloctSource.html");
	}
	
	public void add(){
		MerchRole role = getModel(MerchRole.class,"item",true);
		MerchRole existRole = MerchRole.dao.selectByRoleName(role.getRoleName());
		if(existRole != null) {
			renderFail("角色名已存在");
		}else {
			role.setUpdateDateTime(FSDate.getDateTime());
			role.save();
			renderSuccess();
		}
	}
	
	public void update(){
		MerchRole role = getModel(MerchRole.class,"item",true);
		role.setUpdateDateTime(FSDate.getDateTime());
		role.update();
		renderSuccess();
	}
	
	@Before(Tx.class)
	public void delete(){
		Integer roleId = getParaToInt("id");
		MerchRole.dao.deleteById(roleId);
		OrgUser.dao.setNullRoleId(roleId);
		renderSuccess(false);
	}
	
	public void saveAlloctSource(){
		String[] sourceGroup = getParaValues("sourceGroup");
		String[] sourceUrl = getParaValues("sourceUrl");
		if(sourceGroup == null){
			sourceGroup = new String[]{};
		}
		if(sourceUrl == null){
			sourceUrl = new String[]{};
		}
		MerchRole role = getModel(MerchRole.class,"item",true);
		String group = "";
		for (String tmp : sourceGroup) {
			group += tmp + ",";
		}
		String url = "";
		for (String tmp : sourceUrl) {
			url += tmp + ",";
		}
		role.setSourceGroup(group);
		role.setSourceUrl(url);
		role.update();
		renderSuccess();
	}
	
}
